<template>
  <div>
    <tableComponents
      :isShow="false"
      @toAdd="add_data"
      :isHaveExport="false"
      :isHavePagination="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          stripe
          v-loading="loading"
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
        >
          <el-table-column label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input
                @change="sortNew(scope.$index, scope.row)"
                v-model.number="scope.row.sort"
                size="small"
                style="width: 80px"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column label="图标" min-width="70">
            <template slot-scope="scope">
              <div class="img_bor">
                <img :src="$fnc.getImgUrl(scope.row.piclink)" alt />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="100">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="副标题" min-width="100">
            <template slot-scope="scope">{{ scope.row.title_sub }}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button
                  :loading="$store.state.isLoading"
                  size="mini"
                  @click="editbtn(scope.row)"
                  type="primary"
                  >编辑</el-button
                >
                <el-button
                  :loading="$store.state.isLoading"
                  size="mini"
                  type="danger"
                  @click="deletebtn(scope.row.id)"
                  >删除</el-button
                >
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>
    <el-dialog
      :close-on-click-modal="false"
      title="编辑"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="live_single" label-width="120px" ref="form">
        <el-form-item label="标题：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="live_single.title"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="副标题：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="live_single.title_sub"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图标：">
          <el-col :span="16">
            <el-input
              type="text"
              clearable
              disabled
              v-model="live_single.piclink"
            ></el-input>
            <uploadpic
              :piclink="live_single.piclink"
              :width="150"
              :height="150"
              :real_width="100"
              :real_height="100"
              :is_more="false"
              @send_pic="onSuccess"
            ></uploadpic>
          </el-col>
        </el-form-item>

        <el-form-item label-width="120px">
          <el-button
            :loading="$store.state.isLoading"
            type="primary"
            @click="editlive(live_single)"
            >编 辑</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import uploadpic from "@/components/page/pic/up_pic";
export default {
  data() {
    return {
      is_edit: false,
      dialogFormVisible: false,
      live_single: {},
      current: "",
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      row: {},
      sort: "",
      multipleSelection: [],
      showCity: true,
    };
  },
  components: {
    uploadpic,
  },
  methods: {
    onSuccess(pic) {
      this.live_single.piclink = pic;
    },
    sortNew(index, row) {
      var params = {};
      params = row;
      params.id = row.id;
      params.sort = row.sort;
      this.$api.getStation.vip_system_cate_saveedit(params).then((res) => {
        if (res.data.code == 200 && res.data.result) {
          return;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    deletebtn(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getStation.vip_system_cate_del({ id: id }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            }
          });
        }
      });
    },
    add_data() {
      this.live_single = {
        title: "",
        piclink: "",
        title_sub: "",
      };
      this.is_edit = false;
      this.dialogFormVisible = true;
    },
    editlive() {
      var params = JSON.parse(JSON.stringify(this.live_single));
      if (this.is_edit == false) {
        this.$api.getStation.vip_system_cate_saveadd(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      } else {
        this.$api.getStation.vip_system_cate_saveedit(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    editbtn(item) {
      this.is_edit = true;
      this.live_single = JSON.parse(JSON.stringify(item));
      this.dialogFormVisible = true;
    },
    receive_search(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getStation.vip_system_cate_lists(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
    reload() {
      this.getNewsAll(this.searchAll);
    },
  },
  created() {
    this.getNewsAll();
  },
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
